package com.rc.saas.tenant.service.invoice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.mapper.invoice.InvoiceApplyMapper;
import com.rc.saas.tenant.mapper.invoice.InvoiceMapper;
import com.rc.saas.tenant.mapper.order.OrderInfoMapper;
import com.rc.saas.tenant.model.invoice.Invoice;
import com.rc.saas.tenant.model.invoice.InvoiceApply;
import com.rc.saas.tenant.model.invoice.InvoiceOrder;
import com.rc.saas.tenant.model.invoice.InvoiceOrderExample;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.search.invoice.InvoiceSearch;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.vo.invoice.CountInvoice;
import com.rc.saas.tenant.vo.invoice.InvoiceApplyPrintVo;
import com.rc.saas.tenant.vo.invoice.InvoiceApplyResultVo;
import com.rc.saas.tenant.vo.order.OrderPrintVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author frog
 * @date 2021/3/19
 */
@Service
public class InvoiceApplyServiceImpl implements InvoiceApplyService {

    @Autowired
    private InvoiceApplyMapper invoiceApplyMapper;

    @Autowired
    private InvoiceOrderService invoiceOrderService;

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;


    @Override
    public InvoiceApplyMapper getMapper() {
        return invoiceApplyMapper;
    }

    @Override
    public List<InvoiceApplyPrintVo> exportInvoiceApply(InvoiceSearch invoiceSearch) {
        List<InvoiceApplyPrintVo> list = invoiceApplyMapper.exportInvoiceApply(invoiceSearch);
        return list;
    }

    @Override
    public List<OrderPrintVo> exportInvoiceOrder(Long invoiceApplyId) {
        InvoiceOrderExample example = new InvoiceOrderExample();
        example.createCriteria().andInvoiceApplyIdEqualTo(invoiceApplyId);
        List<InvoiceOrder> invoiceOrders = invoiceOrderService.selectByExample(example);
        List<OrderPrintVo> orderPrintVos = orderInfoMapper.exportListOrderByinvoice(invoiceOrders);
        return orderPrintVos;
    }

    @Override
    public void saveReview(Long invoiceApplyId, String status) {
        InvoiceApply invoiceApply = invoiceApplyMapper.selectByPrimaryKey(invoiceApplyId);
        invoiceApply.setStatus(status);
        Date date = new Date();
        invoiceApply.setLastUpdatedTime(date);
        if ("1".equals(status)) {

            invoiceApplyMapper.updateByPrimaryKeySelective(invoiceApply);
            Invoice invoice = new Invoice();
            BeanUtils.copyProperties(invoiceApply, invoice);
            SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
            invoice.setInvoiceDate(format.format(new Date()));

            invoiceMapper.insertReturnId(invoice);

            InvoiceOrderExample example = new InvoiceOrderExample();
            example.createCriteria().andInvoiceApplyIdEqualTo(invoiceApply.getInvoiceApplyId());
            List<InvoiceOrder> invoiceOrders = invoiceOrderService.selectByExample(example);

            if (!CollectionUtils.isEmpty(invoiceOrders)) {
                for (InvoiceOrder invoiceOrder : invoiceOrders) {
                    invoiceOrder.setUpdateFlag("0");
                    invoiceOrder.setInvoiceId(invoice.getInvoiceId());
                    invoiceOrderService.updateByPrimaryKeySelective(invoiceOrder);

                    OrderInfo orderInfo = new OrderInfo();
                    orderInfo.setOrderId(invoiceOrder.getOrderId());
                    orderInfo.setInvoiceId(invoice.getInvoiceId());
                    orderInfoMapper.updateByPrimaryKeySelective(orderInfo);
                }
            }

        } else if ("2".equals(status)) {
            invoiceApplyMapper.updateByPrimaryKeySelective(invoiceApply);
//            InvoiceOrderExample example = new InvoiceOrderExample();
//            example.createCriteria().andInvoiceApplyIdEqualTo(invoiceApply.getInvoiceApplyId());
//            List<InvoiceOrder> invoiceOrders = invoiceOrderService.selectByExample(example);
//
//            if (!CollectionUtils.isEmpty(invoiceOrders)) {
//                for (InvoiceOrder invoiceOrder : invoiceOrders) {
//                    invoiceOrderService.deleteByPrimaryKey(invoiceOrder.getOrderId());
//                }
//            }
        }
    }

    @Override
    public PageInfo<InvoiceApplyResultVo> listPageInvoiceApply(InvoiceSearch invoiceSearch) {
        /*List<Long> list = invoiceOrderService.listApplyByOrder(invoiceSearch.getOrderNum());
        invoiceSearch.setApplyIdList(list);*/
        PageHelper.startPage(invoiceSearch.getOffset(), invoiceSearch.getLimit());
        List<InvoiceApplyResultVo> enterpriseMemberApplyResults = invoiceApplyMapper.listInvoiceApply(invoiceSearch);
        return new PageInfo<InvoiceApplyResultVo>(enterpriseMemberApplyResults);
    }

    @Override
    public CountInvoice countInvoiceByStation(Long elecStationId,String tenantCode,String tenantOperatorCode) {
        return invoiceApplyMapper.countInvoiceByStation(elecStationId,tenantCode,tenantOperatorCode);
    }

    @Override
    public PageInfo<InvoiceApplyResultVo> listPageInvoiceApplyByProxy(InvoiceSearch invoiceSearch) {
        PageHelper.startPage(invoiceSearch.getOffset(), invoiceSearch.getLimit());
        List<InvoiceApplyResultVo> enterpriseMemberApplyResults = invoiceApplyMapper.listPageInvoiceApplyByProxy(invoiceSearch);
        return new PageInfo<InvoiceApplyResultVo>(enterpriseMemberApplyResults);
    }

    @Override
    public void addInvoiceApply(InvoiceApply invoiceApply, String orderNum) {
        OrderInfo orderInfo = orderInfoMapper.selectOrderByOrderNum(orderNum);

        invoiceApply.setMemberId(orderInfo.getMemberId());
        invoiceApplyMapper.insertReturnId(invoiceApply);

        InvoiceOrder invoiceOrder = new InvoiceOrder();
        invoiceOrder.setOrderId(orderInfo.getOrderId());
        invoiceOrder.setTenantCode(invoiceApply.getTenantCode());
        invoiceOrder.setInvoiceApplyId(invoiceApply.getInvoiceApplyId());
        invoiceOrder.setMemberId(orderInfo.getMemberId());
        invoiceOrder.setUpdateFlag("1");
        invoiceOrderService.insertSelective(invoiceOrder);
    }

    @Override
    public InvoiceApply selectByKey(Long id) {
        return invoiceApplyMapper.selectByKey(id);
    }
}
